import { createRouter, createWebHistory } from 'vue-router';
import LoginForm from '../components/LoginForm.vue';
import HomePage from '../views/HomePage.vue';

const routes = [
    {
        path: '/',
        name: 'Login',
        component: LoginForm
    },
    {
        path: '/home',
        name: 'Home',
        component: HomePage
    }
];

const router = createRouter({
    history: createWebHistory(),
    routes
});

router.beforeEach((to, from, next) => {
    const userInfo = localStorage.getItem('userInfo');
    if (to.path !== '/' && !userInfo) {
        next('/');
    } else {
        next();
    }
});

export default router;